package com.gxg.dao;

import com.gxg.entities.ExperimentalCheckWork;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class ExperimentalCheckWorkDao
{
  @Autowired
  private JdbcTemplate jdbcTemplate;
  
  public String insertSignIn(ExperimentalCheckWork experimentalCheckWork)
  {
    String name = null;
    try
    {
      String sql = "select studentName from experimentalcheckwork where signInTime=? and courseId=? and studentId=?";
      name = (String)this.jdbcTemplate.queryForObject(sql, String.class, new Object[] { experimentalCheckWork.getSignInTime(), experimentalCheckWork.getCourseId(), experimentalCheckWork.getStudentId() });
    }
    catch (Exception localException) {}
    if (experimentalCheckWork.getStudentName().equals(name)) {
      return "请勿重复签到";
    }
    String sql = "insert into experimentalcheckwork value(?,?,?,?)";
    
    this.jdbcTemplate.update(sql, new Object[] {experimentalCheckWork
      .getStudentId(), experimentalCheckWork
      .getStudentName(), experimentalCheckWork
      .getCourseId(), experimentalCheckWork
      .getSignInTime() });
    
    return "签到成功";
  }
  
  public List<ExperimentalCheckWork> getCheckWorkDateByDateString(String dateString, String courseId)
  {
    String sql = "select * from experimentalcheckwork where signInTime>=? and signInTime<=? and courseId=?";
    List<ExperimentalCheckWork> list = this.jdbcTemplate.query(sql, new RowMapper()
    {
      public ExperimentalCheckWork mapRow(ResultSet resultSet, int i)
        throws SQLException
      {
        ExperimentalCheckWork experimentalCheckWork = new ExperimentalCheckWork();
        experimentalCheckWork.setSignInTime(resultSet.getString("signInTime"));
        experimentalCheckWork.setStudentName(resultSet.getString("studentName"));
        experimentalCheckWork.setStudentId(resultSet.getString("studentId"));
        experimentalCheckWork.setCourseId(resultSet.getString("courseId"));
        return experimentalCheckWork;
      }
    }, new Object[] { dateString + "-1", dateString + "-" + 31, courseId });
    
    return list;
  }
}
